import plugin from '../../../lib/plugins/plugin.js'
import Yaml from '../Yaml/Yaml.js'
import {segment} from "oicq";
import fetch from "node-fetch";
import _ from "underscore"

let rootPath = process.cwd()
let pluginPath = rootPath + '/plugins/fisticuffs-plugin/config/'
let appConfigPath = pluginPath + 'application.yaml'
let arseholeConfigPath = pluginPath + 'arsehole.yaml'
let regExp = '^(我|我们|你|你们|他|他们|她|她们|它|它们)(全|都|全都|全部都|)是(我|我们|你|你们|他|他们|她|她们|它|它们)(.*?)$'

const checkAbsoluteMasterFalse = async function (e, absoluteMaster) {
    if (absoluteMaster === null) {
        let msg = '请先发送【设置绝对权限】再使用功能！'
        if (e.isGroup) {
            e.reply([segment.at(e.user_id), msg])
        } else {
            e.reply(msg)
        }
        return false;
    }
    return true;
}

export class help extends plugin {
    constructor() {
        super({
            /** 功能名称 */
            name: '跟屁虫',
            /** 功能描述 */
            dsc: '气死了气死了气死了',
            /** https://oicqjs.github.io/oicq/#events */
            event: 'message',
            /** 优先级，数字越小等级越高 */
            priority: 4995,
            rule    : [
                {
                    /** 命令正则匹配 */
                    reg: regExp,
                    /** 执行方法 */
                    fnc: 'arsehole'
                }
            ]
        });
    }

    async arsehole(e) {

        let arseholeConfig = await Yaml.read(arseholeConfigPath)
        let appConfig = await Yaml.read(appConfigPath)
        let absoluteMaster = appConfig.absoluteMaster
        let arsehole = appConfig.arsehole

        if (!await checkAbsoluteMasterFalse(e, absoluteMaster)) {
            return true
        }

        if (!arsehole) {
            return true
        }

        if (e.user_id !== 834723015) {
            return true
        }

        let matchResult = e.msg.match(regExp)
        let subject = matchResult[1]
        let object = matchResult[3]
        let appellation = matchResult[4]

        let transResult = await fetch(`https://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i=${subject}%0A${object}%0A${appellation}`)
        transResult = await transResult.json()

        let subjectEn = transResult['translateResult'][0][0]['tgt']
        let objectEn = transResult['translateResult'][1][0]['tgt']
        let appellationEn = transResult['translateResult'][2][0]['tgt']

        let subjectLevel = arseholeConfig['person'][`${subjectEn.toLowerCase().replace(' ', '_')}`] || 0
        let objectLevel = arseholeConfig['person'][`${objectEn.toLowerCase().replace(' ', '_')}`] || 0
        let appellationLevel = arseholeConfig['relation'][`${appellationEn.toLowerCase().replace(' ', '_')}`] || 0

        let single = true
        let msg = ''
        if (subjectLevel === 1 && objectLevel === 1) {
            // 我 是 我 【】
            msg = '是就是呗'
        } else if (subjectLevel === 1 && objectLevel === 2) {
            // 我 是 你 【】
            if (appellationLevel === 1) {
                // 长辈
                msg = e.msg
            } else if (appellationLevel === -1) {
                // 晚辈
                msg = `对，${object}是${subject}${appellation}`
            }
        } else if (subjectLevel === 1 && objectLevel === 3) {
            // 我 是 他 【】
            if (appellationLevel === 1) {
                msg = '跟我有鸡毛关系，at他呀'
            } else if (appellationLevel === -1) {
                msg = '牛逼'
            }
        } else if (subjectLevel === 2 && objectLevel === 1) {
            // 你 是 我 【】
            if (appellationLevel === 1) {
                // 长辈
                msg = `对，${object}是${subject}${appellation}`
            } else if (appellationLevel === -1) {
                // 晚辈
                msg = e.msg
            }
        } else if (subjectLevel === 2 && objectLevel === 2) {
            // 你 是 你 【】
            msg = e.msg
        } else if (subjectLevel === 2 && objectLevel === 3) {
            // 你 是 他 【】
            if (appellationLevel === 1) {
                // 长辈
                single = false
                msg = '这可是他说的啊'
            } else if (appellationLevel === -1) {
                // 晚辈
                msg = `不对，你是我${appellation}`
            }
        } else if (subjectLevel === 3 && objectLevel === 1) {
            // 他 是 我 【】
            if (appellationLevel === 1) {
                msg = '厉害'
            } else if (appellationLevel === -1) {
                msg = '跟我有鸡毛关系，at他呀'
            }
        } else if (subjectLevel === 3 && objectLevel === 2) {
            // 他 是 你 【】
            if (appellationLevel === 1) {
                // 长辈
                msg = `不对，我是你${appellation}`
            } else if (appellationLevel === -1) {
                // 晚辈
                single = false
                msg = '这可是他说的啊'
            }
        } else if (subjectLevel === 3 && objectLevel === 3) {
            msg = '咸吃萝卜淡操心'
        }

        if (_.isEmpty(msg)) {
            return true
        }

        if (e.isGroup) {
            if (single) {
                e.reply([segment.at(e.user_id), `\n${msg}`])
            } else {
                e.reply([getAt(e), `\n${msg}`])
            }
        } else {
            e.reply(msg)
        }
        return false;
    }

}

function getAt(e) {
    let atUser = []
    for (let i = 0; i < e.message.length; i++) {
        let messageItem = e.message[i]
        if (messageItem.type === 'at') {
            if (messageItem.qq === e.self_id) {
                continue
            }
            atUser.push(messageItem.qq)
        }
    }
    let at = []
    for (let qq of atUser) {
        at.push(segment.at(qq))
    }

    return at
}
